set more off

use dataset, clear

keep if abs(x) < 100

local nreps 500
local covariates pctwhite pct65plus pcths lmedianincome logvap2014
 
local Z pcteligible2013

su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')

gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

foreach x in z Z Zz {
	gen `x'B = ""
	gen `x'CI = ""
}
foreach x in z Z Zz {
	gen `x'N = ""
	gen `x'Ns = ""
	gen `x'bw = ""
	gen `x'cov = ""
	gen `x'rsq = ""
}

gen index = _n

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest1
program define myest1
	args i bw cov
	mat bootresults = e(bootresults)
	svmat bootresults
	foreach x in z {
		replace `x'B = string(_b[`x'], "%20.02fc") if index == `i'
		_pctile bootresults4 if bootresults3 == 0, nq(1000)
		replace `x'CI = "[" + string(r(r25), "%20.02fc") + ", " + string(r(r975), "%20.02fc") + "]" if index == `i'
		replace `x'N = string(e(N), "%20.00fc") if index == `i'
		replace `x'Ns = string(e(N_stateabbr), "%20.00fc") if index == `i'
		replace `x'bw = "`bw'" if index == `i'
		replace `x'cov = "`cov'" if index == `i'
		replace `x'rsq = string(e(r2), "%20.02fc") if index == `i'
	}	
	drop bootresults*

end

* with interactions

cgmwildboot dpctui20142013 z x zx if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
myest1 1 "100 Miles" "No"

cgmwildboot dpctui20142013 z x zx  `covariates' if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
myest1 2 "100 Miles" "Yes"

cgmwildboot dpctui20152013 z x zx  if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
myest1 3 "100 Miles" "No"

cgmwildboot dpctui20152013 z x zx  `covariates' if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
myest1 4 "100 Miles" "Yes"

* output

keep if index < 5
keep *B *CI* *N *Ns *bw *cov *rsq
l

sxpose, clear force
keep if !mi(_var1)

gen rowname = ""
replace rowname = "Medicaid expansion" if _n == 1
replace rowname = "Number of Counties" if _n == 3
replace rowname = "Number of States" if _n == 4
replace rowname = "Window" if _n == 5
replace rowname = "Covariates" if _n == 6
replace rowname = "R-squared" if _n == 7
order rowname 
keep if _n <= 11

#delimit;

listtex 
	using "_output/tableA10.tex"
	,
	replace
	type rstyle(tabular)
	head(
		\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}
		\toprule &
		\multicolumn{2}{c}{\underline{Percent uninsured}} & 
		\multicolumn{2}{c}{\underline{Percent uninsured}}  \\
		& \multicolumn{2}{c}{\underline{2014-2013}} &
		\multicolumn{2}{c}{\underline{2015-2014}}  \\
		&(1)&(2)&(3)&(4)\\
		\midrule
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

l
